<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="css/template.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HappyMeal Management System</title>
</head>

<body>
<div id='all'>
  <div id='all-wrap'> 
    
    <!--header begin-->
    <div id='wrap-header'>
      <?php
			include 'header.php';
		?>
    </div>
    <!--header end--> 
    
    <!--wrap-body begin-->
    <div id='wrap-body'> 
      <!-- wrap-content begin-->
      <div id='wrap-content'> 
        <!-- nav begin -->
        <div id='nav' style="padding-left:30px;">
          <h4>Current : <a href='main.php'>Main</a>
            <?php include 'nav_png.php'; ?>
            Restaurant Comment</h4>
        </div>
        <!-- nav END --> 
        
        <!-- wrap-search begin -->
        <div id='wrap-search'> 
          <!-- No search module implemented for search--> 
        </div>
        <!-- wrap-search end --> 
        
        <!-- check if it is add or edit or view or delete -->
        <?php
				$show_alert = true;
				if(isset($_POST['restaurant_comment_add'])){
					// restaurant_comment_add for debug purposes
					$user			= $_POST['user'];
					$restaurant		= $_POST['restaurant'];
					$rating			= $_POST['rating'];
					$comment		= $_POST['comment'];
					//Datetime format : date ("Y-m-d H:i:s");
					$create_date		= date("Y-m-d H:i:s");
					$is_blocked			= $_POST['is_blocked'];

					//Prevent Double entering
					$sql_check = "SELECT * FROM restaurant_comment WHERE user='$user' AND restaurant='$restaurant' AND comment='$comment'";
					$sql_check_result = mysql_query($sql_check);
					if(mysql_num_rows($sql_check_result) < 1){
     					//Partner does not exist
						$sql_add = "INSERT INTO restaurant_comment (user,restaurant,rating,comment,create_date,is_blocked) VALUES ('$user','$restaurant','$rating','$comment','$create_date','$is_blocked')";
						$sql_add_result = mysql_query($sql_add);
						if($sql_add_result){
							$message = "Successfully add new restaurant comment";
						}else{
							$message = "Fail to add new restaurant comment - SQL ERROR";
						}
   					}else{
						//Partner already exists
						$message = "Fail to add new restaurant comment - COMMENT ALREADY EXISTS";
					}
				}else if(isset($_POST['restaurant_comment_edit'])){

					// restaurant_edit
					$id 			= $_POST['id'];
					$user			= $_POST['user'];
					$restaurant		= $_POST['restaurant'];
					$rating			= $_POST['rating'];
					$comment		= $_POST['comment'];
					$is_blocked		= $_POST['is_blocked'];

					$sql_edit = "UPDATE restaurant_comment SET ";
					$sql_edit = $sql_edit."user='$user',restaurant='$restaurant',rating='$rating',comment='$comment',is_blocked='$is_blocked' ";
					$sql_edit = $sql_edit."WHERE id='$id'";
					if(mysql_query($sql_edit)){
						//successful
						$message = "Successfully update restaurant comment with id = ".$id;
						//echo "success";
					}else{
						$message = "Fail to update restaurant comment with id = ".$id;
						//echo "fail";
					}
					
				}else if(isset($_POST['restaurant_comment_delete'])){
					// restaurant_delete
					$id = $_POST['id'];
					$sql_delete01 = "DELETE FROM restaurant_comment WHERE id='$id'";
					if(mysql_query($sql_delete01)){
						//successful
						$message = "Successfully delete restaurant comment with id = ".$id;
					}else{
						$message = "Fail to delete restaurant comment with id = ".$id;
					}
				}else{
					// view all category only
					$show_alert = false;
				}
				
				if($show_alert){
	            	echo "<script type='text/javascript'>confirm(";
					echo json_encode($message);
					echo ");</script>";
				}
			?>
        
        <!-- Add button -->
        <h4 align="right" style="padding-right:20px;">
          <?php
				// Allow only admin user to add a new restaurant comment
				if($_SESSION['is_admin'] == 1){
            		echo "<a href='restaurant_comment_add.php'>Add a new restaurant comment</a>";
				}
            ?>
        </h4>
        
        <!-- Print table of categories -->
        <?php
				if($_SESSION['is_admin'] == 1){
					// Display all restaurants when the user is an administrator
					$sql_view = "SELECT * FROM restaurant_comment ORDER BY id DESC";
				}else{
					// Display only logined user's restaurant
					$current_user_id = $_SESSION['id'];
					$sql_view = "SELECT * FROM restaurant_comment WHERE restaurant='$current_user_id' ORDER BY id DESC";
				}
				
				// Execute Query
				$result_view = mysql_query($sql_view);
				
				echo "<table id='partner_table'>";
				echo "<tr>";
				if($_SESSION['is_admin'] == 1){
					// Displaying style for admin
					echo "<th style='width: 25%;'>Restaurant</th>";
					echo "<th style='width: 15%;'>User</th>";
					echo "<th style='width: 5%;'>Rating</th>";
					echo "<th style='width: 40%;'>Comment</th>";
					echo "<th style='width: 10%;'>Blocked?</th>";
					echo "<th style='width: 5%;'>Edit</th>";
				}else{
					// Displaying Style for admin
					echo "<th style='width: 25%;'>User</th>";
					echo "<th style='width: 5%;'>Rating</th>";
					echo "<th style='width: 60%;'>Comment</th>";
					echo "<th style='width: 10%;'>Blocked?</th>";
				}
				
				echo "</tr>";
				while($row = mysql_fetch_array($result_view)){
					$id 		= $row['id'];
					$user 		= $row['user'];
					$rating		= $row['rating'];
					$restaurant	= $row['restaurant'];
					$comment	= $row['comment'];			
					$is_blocked	= $row['is_blocked'];
					
					
					echo "<tr>";
					//Meal Field 
					if($_SESSION['is_admin'] == 1){
						echo "<td align='center'>";
						
						$sql_restaurant_name = "SELECT * FROM restaurant WHERE id='$restaurant'";
						$sql_restaurant_name_result = mysql_query($sql_restaurant_name);
						$sql_restaurant_name_result_row = mysql_fetch_array($sql_restaurant_name_result);
						$restaurant_name = $sql_restaurant_name_result_row['name'];

						echo $restaurant_name;
						
						echo "</td>";
					}
					//Meal Field End
					
					//User Field
					$sql_user_name = "SELECT * FROM user WHERE id='$user'";
					$sql_user_name_result = mysql_query($sql_user_name);
					$sql_user_name_result_row = mysql_fetch_array($sql_user_name_result);
					$user_name = $sql_user_name_result_row['name'];
					echo "<td align='center'>".$user_name."</td>";
					//User Field End
					
					//Rating Field
					echo "<td align='center'>".$rating."</td>";
					//Rating Field End
					
					//Comment Field
					echo "<td align='center'>".$comment."</td>";
					//Comment Field End
					
					//Is Blocked Field
					if($is_blocked == 0){
						//not blocked
						echo "<td align='center'>No</td>";
					}else{
						//blocked
						echo "<td align='center'>Yes</td>";
					}
					//I Blocked Field End
					
					//Edit Field
					if($_SESSION['is_admin'] == 1){
						//echo "<td align='center'><a href='restaurant_comment_edit.php?id=".$id."'>Edit</a></td>";
						echo "<td align='center'><input type='button' name='user_edit_btn' value='Edit' onclick='window.location=\"restaurant_comment_edit.php?id=".$id."\";' />";

					}
					//Edit Field End
					
					echo "</tr>";
				}
				echo "</table>";
			?>
      </div>
      <!-- wrap-content end--> 
      
    </div>
    <!--wrap-body end--> 
    
    <!--wrap-footer begin-->
    <div id='wrap-footer'>
      <?php
			include 'footer.php';
		?>
    </div>
    <!--wrap-footer end--> 
    
  </div>
  <!--all-wrap end--> 
</div>
<!--all end-->
</body>
</html>